//
//  UIViewController+DR.m
//  DoctorRen
//
//  Created by Macx on 2018/11/20.
//

#import "UIViewController+DR.h"
#import "DRWebViewController.h"
#import "DRTabBarController.h"
#import "DRGuideViewController.h"
#import "DRLoginViewController.h"

@implementation UIViewController (DR)

-(void)pushWebViewWithUrl:(NSString *)url{
    
//    if ([DRLoginDataModel getUserToken].length == 0 ) {
//        [self presentNeedLogin];
//        return;
//    }
    
    DRWebViewController *webView = [[DRWebViewController alloc] init];
    webView.view.backgroundColor = [UIColor whiteColor];
    webView.url = url;
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:webView animated:YES];
    self.hidesBottomBarWhenPushed = NO;
    
}

-(void)pushDevicesVC{
    UIViewController *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"DRDevicesVC"];
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
    self.hidesBottomBarWhenPushed = NO;
}

-(void) presentBindDeviceVC:(NSInteger) type block:(DRDeviceValueBlock) block{
    DRBindDeviceVC *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"DRBindDeviceVC"];
    vc.type = type;
    vc.block = block;
    [self presentViewController:vc animated:YES completion:nil];
}

/** 单个按键的 alertView */
- (void)presentAlertViewWithTitle:(NSString *)title message:(NSString *)message confirmTitle:(NSString *)confirmTitle handler:(void(^)(void))handler {
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    
    // creat action
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:confirmTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
        if (handler) {
            handler();
        }
    }];
    
    // add acton
    [alert addAction:cancelAction];
    
    // present alertView on rootViewController
    dispatch_async(dispatch_get_main_queue(), ^{
        [self presentViewController:alert animated:YES completion:nil];
    });
}

/** 双按键的 alertView */
- (void)presentAlertViewWithTitle:(NSString *)title message:(NSString *)message cancelTitle:(NSString *)cancelTitle defaultTitle:(NSString *)defaultTitle distinct:(BOOL)distinct cancel:(void(^)(void))cancel confirm:(void(^)(void))confirm {
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    
    if (distinct) {
        // 左浅右深
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            if (cancel) {
                cancel();
            }
        }];
        [alert addAction:cancelAction];
    } else {
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            if (cancel) {
                cancel();
            }
        }];
        [alert addAction:cancelAction];
    }
    
    
    UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:defaultTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if (confirm) {
            confirm();
        };
    }];
    
    [alert addAction:defaultAction];
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [self presentViewController:alert animated:YES completion:nil];
    });
    
}


/** Alert  任意多个按键 返回选中的 buttonIndex 和 buttonTitle */
- (void)presentAlertWithTitle:(NSString *)title message:(NSString *)message actionTitles:(NSArray *)actionTitles  preferredStyle:(UIAlertControllerStyle)preferredStyle handler:(void(^)(NSUInteger buttonIndex, NSString *buttonTitle))handler {
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:preferredStyle];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        handler(0, @"取消");
    }];
    [alert addAction:cancelAction];
    
    for (int i = 0; i < actionTitles.count; i ++) {
        
        UIAlertAction *confimAction = [UIAlertAction actionWithTitle:actionTitles[i] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            handler((i + 1), actionTitles[i]);
        }];
        [alert addAction:confimAction];
    }
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [self presentViewController:alert animated:YES completion:nil];
    });
}

-(void)gotoLogin{
    UIViewController *loginVC = [UIStoryboard storyboardWithName:@"Login" bundle:nil].instantiateInitialViewController;
    KEY_WINDOW.rootViewController = loginVC;
    [KEY_WINDOW makeKeyAndVisible];
    
}

-(void)setKefuButtonHidden:(BOOL)isHidden{
    if ([KEY_WINDOW.rootViewController isKindOfClass:[DRTabBarController class]]) {
        DRTabBarController *tab = (DRTabBarController *)KEY_WINDOW.rootViewController;
        tab.kfButtonIsHidden = isHidden;
    }
    
}

-(void)presentGuideView{
    DRGuideViewController *guideVC = [[DRGuideViewController alloc] init];
    
    //把当前控制器作为背景
    self.definesPresentationContext = YES;
    
    //设置模态视图弹出样式
    guideVC.modalPresentationStyle = UIModalPresentationOverFullScreen;
    guideVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:guideVC animated:YES completion:nil];

}

-(void)presentNeedLogin:(BOOL)needShowAlert{
    WEAKSELF
    dispatch_block_t block = ^{
        STRONGSELF
        UINavigationController *loginNav = [UIStoryboard storyboardWithName:@"Login" bundle:nil].instantiateInitialViewController;
        [strongSelf presentViewController:loginNav animated:YES completion:nil];
    };
    
    if (needShowAlert) {
        [self presentAlertViewWithTitle:@"需要登录才能查看！" message:@"" confirmTitle:@"确定" handler:^{
            dispatch_async(dispatch_get_main_queue(), ^{
                block();
            });
        }];
    }else{
        dispatch_async(dispatch_get_main_queue(), ^{
            block();
        });
    }


}

-(void) showAlert:(NSString *)title detail:(NSString *) detail items:(NSArray <MMPopupItem *>*) items{
    
    MMAlertView *alertView = [[MMAlertView alloc] initWithTitle:title detail:detail items:items];
    alertView.attachedView = KEY_WINDOW;
    alertView.attachedView.mm_dimBackgroundBlurEnabled = NO;
    [alertView show];
}

@end
